﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace YJ.Data.MSSQL
{
    public class MenuUser : YJ.Data.Interface.IMenuUser
    {
        private DBHelper dbHelper = new DBHelper();
        /// <summary>
        /// 构造函数
        /// </summary>
        public MenuUser()
        {
        }
        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.MenuUser实体类</param>
        /// <returns>操作所影响的行数</returns>
        public int Add(YJ.Data.Model.MenuUser model)
        {
            string sql = @"INSERT INTO MenuUser
				(ID,MenuID,SubPageID,Organizes,Users,Buttons,Params) 
				VALUES(@ID,@MenuID,@SubPageID,@Organizes,@Users,@Buttons,@Params)";
            SqlParameter[] parameters = new SqlParameter[]{
				new SqlParameter("@ID", SqlDbType.UniqueIdentifier, -1){ Value = model.ID },
				new SqlParameter("@MenuID", SqlDbType.UniqueIdentifier, -1){ Value = model.MenuID },
				new SqlParameter("@SubPageID", SqlDbType.UniqueIdentifier, -1){ Value = model.SubPageID },
				new SqlParameter("@Organizes", SqlDbType.VarChar, 100){ Value = model.Organizes },
				new SqlParameter("@Users", SqlDbType.VarChar, -1){ Value = model.Users },
				model.Buttons == null ? new SqlParameter("@Buttons", SqlDbType.VarChar, -1) { Value = DBNull.Value } : new SqlParameter("@Buttons", SqlDbType.VarChar, -1) { Value = model.Buttons },
				model.Params == null ? new SqlParameter("@Params", SqlDbType.VarChar, -1) { Value = DBNull.Value } : new SqlParameter("@Params", SqlDbType.VarChar, -1) { Value = model.Params }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.MenuUser实体类</param>
        public int Update(YJ.Data.Model.MenuUser model)
        {
            string sql = @"UPDATE MenuUser SET 
				MenuID=@MenuID,SubPageID=@SubPageID,Organizes=@Organizes,Users=@Users,Buttons=@Buttons,Params=@Params
				WHERE ID=@ID";
            SqlParameter[] parameters = new SqlParameter[]{
				new SqlParameter("@MenuID", SqlDbType.UniqueIdentifier, -1){ Value = model.MenuID },
				new SqlParameter("@SubPageID", SqlDbType.UniqueIdentifier, -1){ Value = model.SubPageID },
				new SqlParameter("@Organizes", SqlDbType.VarChar, 100){ Value = model.Organizes },
				new SqlParameter("@Users", SqlDbType.VarChar, -1){ Value = model.Users },
				model.Buttons == null ? new SqlParameter("@Buttons", SqlDbType.VarChar, -1) { Value = DBNull.Value } : new SqlParameter("@Buttons", SqlDbType.VarChar, -1) { Value = model.Buttons },
				model.Params == null ? new SqlParameter("@Params", SqlDbType.VarChar, -1) { Value = DBNull.Value } : new SqlParameter("@Params", SqlDbType.VarChar, -1) { Value = model.Params },
				new SqlParameter("@ID", SqlDbType.UniqueIdentifier, -1){ Value = model.ID }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 删除记录
        /// </summary>
        public int Delete(Guid id)
        {
            string sql = "DELETE FROM MenuUser WHERE ID=@ID";
            SqlParameter[] parameters = new SqlParameter[]{
				new SqlParameter("@ID", SqlDbType.UniqueIdentifier){ Value = id }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 将DataRedar转换为List
        /// </summary>
        private List<YJ.Data.Model.MenuUser> DataReaderToList(SqlDataReader dataReader)
        {
            List<YJ.Data.Model.MenuUser> List = new List<YJ.Data.Model.MenuUser>();
            YJ.Data.Model.MenuUser model = null;
            while (dataReader.Read())
            {
                model = new YJ.Data.Model.MenuUser();
                model.ID = dataReader.GetGuid(0);
                model.MenuID = dataReader.GetGuid(1);
                model.SubPageID = dataReader.GetGuid(2);
                model.Organizes = dataReader.GetString(3);
                model.Users = dataReader.GetString(4);
                if (!dataReader.IsDBNull(5))
                    model.Buttons = dataReader.GetString(5);
                if (!dataReader.IsDBNull(6))
                    model.Params = dataReader.GetString(6);
                List.Add(model);
            }
            return List;
        }
        /// <summary>
        /// 查询所有记录
        /// </summary>
        public List<YJ.Data.Model.MenuUser> GetAll()
        {
            string sql = "SELECT * FROM MenuUser";
            SqlDataReader dataReader = dbHelper.GetDataReader(sql);
            List<YJ.Data.Model.MenuUser> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }
        /// <summary>
        /// 查询记录数
        /// </summary>
        public long GetCount()
        {
            string sql = "SELECT COUNT(*) FROM MenuUser";
            long count;
            return long.TryParse(dbHelper.GetFieldValue(sql), out count) ? count : 0;
        }
        /// <summary>
        /// 根据主键查询一条记录
        /// </summary>
        public YJ.Data.Model.MenuUser Get(Guid id)
        {
            string sql = "SELECT * FROM MenuUser WHERE ID=@ID";
            SqlParameter[] parameters = new SqlParameter[]{
				new SqlParameter("@ID", SqlDbType.UniqueIdentifier){ Value = id }
			};
            SqlDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<YJ.Data.Model.MenuUser> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List.Count > 0 ? List[0] : null;
        }


        /// <summary>
        /// 删除记录
        /// </summary>
        public int DeleteByOrganizes(string organizes)
        {
            string sql = "DELETE FROM MenuUser WHERE Organizes=@Organizes";
            SqlParameter[] parameters = new SqlParameter[]{
				new SqlParameter("@Organizes", SqlDbType.VarChar){ Value = organizes }
			};
            return dbHelper.Execute(sql, parameters);
        }

        /// <summary>
        /// 删除记录
        /// </summary>
        public int DeleteByMenuID(Guid menuID)
        {
            string sql = "DELETE FROM MenuUser WHERE MenuID=@MenuID";
            SqlParameter[] parameters = new SqlParameter[]{
				new SqlParameter("@MenuID", SqlDbType.UniqueIdentifier){ Value = menuID }
			};
            return dbHelper.Execute(sql, parameters);
        }
    }
}